package com.rayboo.example.thread;

public class SyncThread implements Runnable {
    private static int count;

    public SyncThread() {
        count = 0;
    }

    @Override
    public void run() {
        synchronized (this) {
            try {
                for (int i = 0;i<5;i++) {
                    System.out.println(Thread.currentThread().getName() + ":" + (count++));
                }
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
//        SyncThread syncThread = new SyncThread();
//        Thread thread1 = new Thread(syncThread,"Thread1");
//        Thread thread2 = new Thread(syncThread,"Thread2");
//        thread1.start();
//        thread2.start();

        Thread thread3 = new Thread(new SyncThread(),"Thread3");
        Thread thread4 = new Thread(new SyncThread(),"Thread4");
        thread3.start();
        thread4.start();
    }
}
